package com.lib.TrapDoor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lib.DB.DBHelper;
import com.lib.index.buildIndex;
import com.lib.index.indexHelper;
import com.lib.model.encryptedIndex;



public class GenTrapDoor {
	/**
	 * 得到wordlist中关键字对应的明文陷门
	 * @param wordList
	 * @return
	 */
	public static double [] getSearchPlainIndex(List<String>wordList)
	{
		int disWordCount=indexHelper.wordDisCount;
		int v=10;//
		int u=4*v;//dummuy keyword的总数
		int n=disWordCount+u;
		double []resultIndex=new double[n];
		for(int i=0;i<n;i++)
		{
			resultIndex[i]=0;
		}
		Map<String, Double>idfMaps=DBHelper.getIDFSForWords(wordList);
		double norIdfs=indexHelper.getNorValues(idfMaps);
		Iterator<String> iterator=wordList.iterator();
		while(iterator.hasNext())
		{
			String tempWord=iterator.next();
			int index=indexHelper.wordList.indexOf(tempWord);
			resultIndex[index]=idfMaps.get(tempWord)/norIdfs;//得到量化的ifd值
		//	resultIndex[index]=DBHelper.getIDFForWord(tempWord);
		}
		List<Integer> randomList=EncryptedTroopHelper.getRandom(v);
		Iterator< Integer>dummuyIterator=randomList.iterator();
		while(dummuyIterator.hasNext())
		{
			int dummuyIndex=disWordCount+dummuyIterator.next();
			resultIndex[dummuyIndex]=1;
		}
		return resultIndex;
	}
	/**
	 * 获得加密的trapDoor
	 * @param wordList
	 * @return
	 */
	public static encryptedIndex getEncryptedSearchIndex(List<String> wordList)
	{
		encryptedIndex resultIndex=new encryptedIndex();
		double []plainTrapDoor=getSearchPlainIndex(wordList);
		resultIndex=EncryptedTroopHelper.encrypTropDoor(buildIndex.getEncryptedSK(), plainTrapDoor);
		return resultIndex;
	}
	

}
